define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teachersale/index' + location.search,
                    add_url: 'teachersale/add',
                    // edit_url: 'teachersale/edit',
                    // del_url: 'teachersale/del',
                    multi_url: 'teachersale/multi',
                    table: 'learn_course_order',
                }
            });

            var table = $("#table");
            var  stvisible = '=';
            var  stvisible2 = true;
            if(Config.org_cate_id > 0){
                stvisible = false;
                stvisible2 = false;
            }
            table.on('post-common-search.bs.table', function (event, table) {
                $(".commonsearch-table").find('.btn-success').html('搜索');
                $(".commonsearch-table").find('.btn-default').html('刷新');
                $('.form-commonsearch').attr('autocomplete', 'off');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'tid',
                sortName: 't.id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'tid', title: '序号', operate:false},
                         {field: 'o.id', title: __('单位名称'),visible: stvisible2, operate:stvisible,searchList: $.getJSON('usechart/searchlist') , formatter: Controller.api.formatter.catename},
                        // {field: 'order_sn', title: __('Order_sn')},
                        // {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'o.createtime', title: __('订单时间'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'coupon_id', title: __('Coupon_id')},
                        // {field: 'teacher_id', title: __('Teacher_id')},
                        {field: 't.number', title: '老师编号', operate:'like', formatter: Controller.api.formatter.custom},
                        // {field: 'learncateteacher.user_id', title: __('Learncateteacher.user_id')},
                        // {field: 'learncateteacher.introduce', title: __('Learncateteacher.introduce')},
                        {field: 'tname', title: '老师姓名', operate:false},
                        {field: 'total_money', title: '订单金额', operate:false},
                        {field: 'left_money', title: '待支付金额', operate:false},
                        {field: 'pay_money', title: '已支付金额', operate:false},
                        // {field: 'learncateteacher.status', title: __('Learncateteacher.status')},
                        // {field: 'learncateteacher.createtime', title: __('Learncateteacher.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'learncateteacher.org_cate_id', title: __('Learncateteacher.org_cate_id')},
                        // {field: 'learncateteacher.type', title: __('Learncateteacher.type')},
                        // {field: 'learncateteacher.is_tuijian', title: __('Learncateteacher.is_tuijian')},
                        // {field: 'learncateteacher.sort', title: __('Learncateteacher.sort')},
                        // {field: 'learncateteacher.image', title: __('Learncateteacher.image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'learncateteacher.flag', title: __('Learncateteacher.flag'), formatter: Table.api.formatter.flag},
                        // {field: 'learncateteacher.number', title: __('Learncateteacher.number')},
                        //{field: 'learncourseorder.createtime', title: __('订单时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('订单时间'), operate:'RANGE',visible: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {
                                    name: 'sale',
                                    title: __('查看详情'),
                                    text: "查看详情",
                                    classname: 'btn btn-xs btn-warning btn-addtabs btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: 'teachersale/sale'+ location.search
                                }
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search:false,
                commonSearch:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sale: function () {
            Table.api.init({
                extend: {
                    chapter_url: 'teachersale/sale'+ location.search,
                    table: 'teachersale_sale'
                }
            });
            var table1 = $('#table1');
            table1.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#total_money").text(data.extend.total_money);
                $("#pay_money").text(data.extend.pay_money);
                $("#left_money").text(data.extend.left_money);
            });
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('序号')},
                        {field: 'order_sn', title: __('订单号'),operate:false},
                        {field: 'course.number', title: __('课程编码'),operate:'LIKE'},
                        {field: 'course.name', title: __('课程名称'),operate:false},
                        {field: 'price', title: __('订单金额'),operate:'BETWEEN'},
                        {field: 'type', title: __('支付状态'),operate:false, searchList: {"0":__('待支付'),"1":__('支付成功'),"2":__('支付失败')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('订单时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:false,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return row.tnumber;
                },
                catename: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return row.oname;
                }
            },
        }
    };
    return Controller;
});